package org.geektimes.configuration.microprofile.source;

import org.eclipse.microprofile.config.spi.ConfigSource;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author: jipeng
 * @Description: 基于 Map 数据结构 {@link ConfigSource} 的实现
 * @Date: Created in 2021/3/19 8:14
 */
public abstract class MapBesedConfigSource extends BesedConfigSource {

    private final Map<String,String> source;

    public MapBesedConfigSource(String configSourceName, Integer ordinal) {
        super(configSourceName,ordinal);
        source = getProperties();
    }


    @Override
    public final Map<String, String> getProperties() {
        Map<String, String> configData = new HashMap<>();
        try {
            prepareConfigData(configData);
        } catch (Throwable e) {
            throw new IllegalArgumentException("准备配置数据发送错误", e);
        }
        return Collections.unmodifiableMap(configData);
    }

    /**
     * 准备配置数据
     *
     * @param configData
     */
    protected abstract void prepareConfigData(Map configData) throws Throwable;

    @Override
    public Set<String> getPropertyNames() {
        return source.keySet();
    }

    @Override
    public String getValue(String propertyName) {
        return source.get(propertyName);
    }
}
